package cn.edu.gduf.luowowo.website.web.controller;

import cn.edu.gduf.luowowo.article.domain.Travel;
import cn.edu.gduf.luowowo.article.query.TravelQuery;
import cn.edu.gduf.luowowo.article.service.ITravelService;
import cn.edu.gduf.luowowo.cache.domain.ScenicStatisVO;
import cn.edu.gduf.luowowo.cache.domain.StrategyStatisVO;
import cn.edu.gduf.luowowo.cache.domain.TravelStatisVo;
import cn.edu.gduf.luowowo.cache.service.*;
import cn.edu.gduf.luowowo.comment.domain.ScenicComment;
import cn.edu.gduf.luowowo.comment.domain.TravelComment;
import cn.edu.gduf.luowowo.comment.service.IScenicCommentService;
import cn.edu.gduf.luowowo.comment.service.ITravelCommentService;
import cn.edu.gduf.luowowo.common.exception.LogicException;
import cn.edu.gduf.luowowo.common.util.AjaxResult;
import cn.edu.gduf.luowowo.common.util.Consts;
import cn.edu.gduf.luowowo.common.util.RedisKeys;
import cn.edu.gduf.luowowo.flight.service.IFlightOrderService;
import cn.edu.gduf.luowowo.member.domain.UserInfo;
import cn.edu.gduf.luowowo.member.service.IItermOrderService;
import cn.edu.gduf.luowowo.member.service.IUserInfoService;
import cn.edu.gduf.luowowo.website.annotation.UserParam;
import cn.edu.gduf.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("mine")
public class HomeController {

    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private ITravelService travelService;

    @Reference
    private ITravelCommentService travelCommentService;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private ITravelStatisVoService travelStatisVoService;

    @Reference
    private IStrategyStatisVoRedisService strategyStatisVoRedisService;

    @Reference
    private IScenicStatisVORedisService scenicStatisVORedisService;

    @Reference
    private IHotelRedisService hotelRedisService;

    @Reference
    private IScenicCommentService scenicCommentService;

    @Reference
    private IFlightOrderService flightOrderService;

    @Reference
    private IItermOrderService iItermOrderService;

//我的窝
    @RequestMapping("/home")
    public String home(Model model, @UserParam UserInfo userInfo){
        if(userInfo == null){
            throw new LogicException("请先登录");
        }
        List<Travel> travels = travelService.getTravelByAuthorId(userInfo.getId());
        model.addAttribute("travels",travels);
        model.addAttribute("travelsCount",travels.size());

        List<TravelComment> comments = travelCommentService.queryByAuthorId(userInfo.getId());
        model.addAttribute("comments",comments);

        //关注数
        int concerns = userInfoService.getCountForConcern(userInfo.getId());
        model.addAttribute("concerns",concerns);
        //分数数
        int fans = userInfoService.getCountForFans(userInfo.getId());
        model.addAttribute("fans",fans);
        //回显关注的人
        List<Long> arry = userInfoService.getConcernId(userInfo.getId());
        List<UserInfo> users = userInfoService.queryConcernByIds(arry);
        model.addAttribute("users",users);

        //查找金币数
        int coins = userInfoService.getCountForCoins(userInfo.getId());
        model.addAttribute("coins",coins);

        List<ScenicComment> scenicComments = scenicCommentService.getScenicCommentById(userInfo.getId());
        model.addAttribute("comments",scenicComments);

        return "mine/homepage";
    }

//我的游记controller
    //mytravelnotes
    @RequestMapping("/mytravelnotes")
    public String mytravelnotes(Model model, @UserParam UserInfo userInfo,@ModelAttribute("qo") TravelQuery qo){
        if(userInfo == null){
            throw new LogicException("请先登录");
        }
        List<Travel> travels = travelService.getTravelByAuthorId(userInfo.getId());
        model.addAttribute("travels",travels);
        model.addAttribute("count",travels.size());
        Integer replynum = 0;
        Integer viewnum =0;
        for (Travel travel : travels) {
            replynum = replynum + travel.getReplynum();
            viewnum = viewnum + travel.getViewnum();
        }
        model.addAttribute("replynum",replynum);
        model.addAttribute("viewnum",viewnum);

        PageInfo<Travel> pageInfo = travelService.getPageInfoByAuthorId(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "mine/mytravelnotes";
    }

//我的点评
    //review
    @RequestMapping("/review")
    public String review(Model model,@UserParam UserInfo userInfo){
        if(userInfo == null){
            throw new LogicException("请先登录");
        }
        List<ScenicComment> scenicComments = scenicCommentService.getScenicCommentById(userInfo.getId());
        model.addAttribute("comments",scenicComments);

        return "mine/review";
    }

//我的收藏
    //travelcollection
    @RequestMapping("/travelcollection")
    public String travelcollection(Model model,@UserParam UserInfo userInfo){
        if(userInfo == null){
            throw new LogicException("请先登录");
        }
        //景点收藏
        List<ScenicStatisVO> scenics = scenicStatisVORedisService.getScenicStatisVoByUserId(userInfo.getId());
        model.addAttribute("scenics",scenics);

        //游记收藏
        List<TravelStatisVo> travels =travelStatisVoService.getTravelStatisVoByUserId(userInfo.getId());
        model.addAttribute("travels",travels);

        //攻略收藏
       List<StrategyStatisVO> strategies = strategyStatisVoRedisService.getStrategyStatisVoByUserId(userInfo.getId());
        model.addAttribute("strategies",strategies);

        //旅行社收藏
        model.addAttribute("hotel",hotelRedisService.getHotelFavor(userInfo.getId()));

        return "mine/travelcollection";
    }

//个人设置
    //setting
    @RequestMapping("/setting")
    public String setting(@UserParam UserInfo userInfo,Model model){
        if(userInfo == null){
            throw new LogicException("请先登录");
        }


        return "mine/setting";
    }


//发送原来的手机号码验证
    @RequestMapping("/sendPhoneVerifyCode")
    @ResponseBody
    public Object sendVerifyCodeByUpdate(String phone){
        userInfoService.sendVerifyCode(phone);
        return AjaxResult.SUCCESS;
    }
//发送新的手机号码
    @RequestMapping("/sendNewPhoneVerifyCode")
    @ResponseBody
    public Object sendNewPhoneVerifyCode(String newMobile){
        userInfoService.sendVerifyCode(newMobile);
        return AjaxResult.SUCCESS;
    }
//手机验证验证码是否正确
    @RequestMapping("/checkVerifyCode")
    @ResponseBody
    public Object checkVerifyCode(String phone, String code){
        String key = RedisKeys.VERIFY_CODE.join(phone);
        String windowCode = userInfoRedisService.getVerifyCode(key);
        System.out.println(windowCode);
        if (windowCode == null || !windowCode.equalsIgnoreCase(code)) {
            throw new LogicException("验证码失效或错误");
        }
        return AjaxResult.SUCCESS;
    }
 //验证手机号是否正确
    @RequestMapping("/setNewPhone")
    @ResponseBody
    public Object setNewPhone(String newMobile, String verifyCode,@UserParam UserInfo userInfo){
        String key = RedisKeys.VERIFY_CODE.join(newMobile);
        String windowCode = userInfoRedisService.getVerifyCode(key);
        System.out.println(newMobile);
        System.out.println(windowCode);
        if (windowCode == null || !windowCode.equalsIgnoreCase(verifyCode)) {
            throw new LogicException("验证码失效或错误");
        }
        if (newMobile.equals(userInfo.getPhone())){
            throw new LogicException("新手机号与旧手机号重复了");
        }
        userInfoService.updatePhone(userInfo.getPhone(),newMobile);
        return AjaxResult.SUCCESS;
    }


//在设置中更改数据
@RequestMapping("/updateUser")
@ResponseBody
public AjaxResult updateUser(UserInfo user, HttpServletResponse response, HttpServletRequest request) {

    userInfoService.update(user);
    UserInfo info = userInfoService.get(user.getId());
    String token = CookieUtil.getToken(request);
    String key = RedisKeys.USER_TOKEN.join(token);
    userInfoRedisService.addTokenToRedis(key, info, Consts.USER_INFO_TOKEN_VAI_TIME);
    request.getSession().setAttribute("userInfo", info);

    return AjaxResult.SUCCESS;
}

//我的关注
    @RequestMapping("/focus")
    @ResponseBody
    public Object focus(@UserParam UserInfo userInfo,Long uid){
        try {
            userInfoService.focus(userInfo.getId(),uid);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"关注失败");
        }
        return new AjaxResult(true,"关注成功");
    }

//我的订单
    @RequestMapping("/myorder")
    public Object myorder(Model model,@UserParam UserInfo userInfo){
        if (userInfo == null){
            throw new LogicException("请先登录");
        }
        //查询该用户的所有订单数据
        //机票订单数据
        model.addAttribute("airs", flightOrderService.getFlightOrdersByUserId(userInfo.getId()));
        model.addAttribute("list",iItermOrderService.selectByUserId(userInfo.getId()));
        return "mine/myorder";
    }
}
